package com.app.view.component.room.seat 
{
	import flash.geom.Point;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import com.greensock.TweenLite;
	
	import com.peachcastle.core.Display;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class Paijiu extends Display
	{
		public static const PAIJIU:Array = [
											0,
											66, 65, 64, 63, 62, 61,
											55, 54, 53, 52, 51,
											44, 43, 42, 41, 32,31,
											22, 11, 222, 21
										  ];
										  
		public static const PAIJIU_TYPE:Array = [
													{'66': '天牌'}, 
													{ '11': '地牌'}, { '44': '人牌'}, 
													{ '31': '鹅牌'}, { '55': '梅牌'}, { '222': '長三'},
													{'22': '板凳'}, { '65': '斧頭'}, { '64': '紅頭'},
													{'61': '高腳七'}, { '51': '零霖六'}, { "54": '雜九'},
													{'63': '雜九'}, { '53': '雜八'}, { '62': '雜八'}, 
													{ '43': '雜七'}, { '52': '雜七'}, { '42': ' 大頭六'}, 
													{ '41': '雜五'}, { '32': '雜五'}, {'21': '丁三'}
										];
										
										  
		private var _paiNumber:int = 0;
		private var _topPoint:Point;
		private var _startPoint:Point;
		private var _startRotate:Number = 0;
		
		public function Paijiu(ui:MovieClip) 
		{
			super(ui, true);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_topPoint = new Point();
			_topPoint.x = assest.x;
			_topPoint.y = assest.y - 20;

			_startPoint = new Point();
			_startPoint.x = assest.x;
			_startPoint.y = assest.y;
			
			_startRotate = rotation;
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
		}
		
		public function changePaijiuStatus(isActive:Boolean = true):void
		{
			if (isActive)
			{
				assest.buttonMode = true;
				assest.addEventListener(MouseEvent.CLICK, changePosition);
			}else
			{
				assest.buttonMode = false;
				assest.removeEventListener(MouseEvent.CLICK, changePosition);
			}
		}
		
		public function changePosition(e:MouseEvent):void
		{
			changePaijiuStatus(false);
			
			if (assest.y == _topPoint.y)
			{
				TweenLite.to(assest, .1, { x: _startPoint.x, y: _startPoint.y, onComplete:setPosition, onCompleteParams: [e]} );
			}else
			{
				TweenLite.to(assest, .1, { x: _topPoint.x, y: _topPoint.y, onComplete:setPosition, onCompleteParams: [e]} );
			}
		}
		
		private function setPosition(e:MouseEvent):void
		{
			dispatchEvent(e);
			
			changePaijiuStatus(true);
		}
		
		public function rotatePosition(x:Number, y:Number, r:Number):void
		{
			TweenLite.to(assest, 0.5, { x: x, y: y, rotation: r } );
		}
		
		public function show(data:int, isActive:Boolean = false, isHighlight:Boolean = false):void
		{
			for (var key:* in PAIJIU)
			{
				if (PAIJIU[key] == data)
				{
					if (isHighlight) assest.gotoAndStop("highlight");
					
					assest['pai_number'].gotoAndStop(key + 1);
					
					if (data != 0)
					{
						_paiNumber = data;
						changePaijiuStatus(isActive);
						
						resetPosition();
					}
				}
			}
			
			showAssest();
		}
		
		public function setPaijiuScale(n:Number, index:int = 0):void
		{
			assest.scaleX = n;
			assest.scaleY = n;
			
			if (n == 1)
			{
				resetPosition();
			}else
			{
				if (index == 4) assest.x = assest.x - 5;
			}
		}
		
		public function resetPosition():void
		{
			rotation = _startRotate;
			assest.x = _startPoint.x;
			assest.y = _startPoint.y;
		}
		
		public function resetHighlight():void
		{
			assest.gotoAndStop("normal");
		}
		
		public static function getType(pai:int):Array
		{
			var paiStr:String = String(pai);
			for (var i:String in PAIJIU_TYPE)
			{
				for (var j:String in PAIJIU_TYPE[i])
				{
					if (j == paiStr)
					{
						return [i, PAIJIU_TYPE[i][j]];
					}
				}
			}
			
			var n:int = 0;
			var af:Array = paiStr.split("");
			for (var k:int = 0; k < af.length; k++) 
			{
				n += int(af[k]);
			}
			
			return [-1, String(n % 10)];
		}
		
		public function get paiNumber():int { return _paiNumber; }
		public function get position():int { return assest.y == _startPoint.y ? 0 : 1; }
	}

}